package com.itdaan.portal.business.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.itdaan.portal.business.controller.ShiroDbRealm.ShiroUser;
import com.itdaan.portal.business.domain.CollectionGroup;
import com.itdaan.portal.business.service.CollectionGroupService;
import com.itdaan.portal.common.exception.ViewException;

/**
 * 收藏夹业务控制类
 * @author www.itdaan.com
 */
@Controller
@RequestMapping(value = "/user/collection_group")
@SessionAttributes
public class CollectionGroupController {

	private Logger logger = LoggerFactory.getLogger(CollectionGroupController.class);
	
	@Resource(name = "collectionGroupService")
	private CollectionGroupService collectionGroupService;
	
	@RequestMapping(value = "/login_list.do", method = RequestMethod.GET)
    public
    @ResponseBody
    Map<String,Object> login_list(
            HttpServletRequest request,
            HttpServletResponse response
    		) throws ViewException {
		Map<String, Object> modelAndView = new HashMap<String, Object>();
        try{
    		Subject currentUser = SecurityUtils.getSubject();
    		if(currentUser == null){
    			modelAndView.put("success", false);
    			modelAndView.put("msg", "您未登录，请登录网站");    			
    			return modelAndView;
    		}
    		ShiroUser shiroUser = (ShiroUser)currentUser.getPrincipals().getPrimaryPrincipal();
    		Map<String,Object> params = new HashMap<String, Object>();
    		params.put("userId", shiroUser.getId());
    		List<CollectionGroup> list = collectionGroupService.findCollectionGroups(params);
			modelAndView.put("success", true);
			modelAndView.put("data", list);    		
        }catch(Exception e){
			logger.error("后台业务逻辑出现异常", e);
			modelAndView.put("success", false);
			modelAndView.put("msg", "网站出现异常等待程序员GG修复。。。");
    	}
		return modelAndView;
	}
}
